package com.yyk.yykywd.ui.work;

import android.text.TextUtils;
import android.widget.EditText;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkDelplanInfo;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.ToastUtil;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 删除计划
 */
public class DeletePlanActivity extends BaseActivity {

    @BindView(R.id.et_delremark)
    EditText etDelremark;
    private String id; //业务ID

    @Override
    public int getLayoutId() {
        return R.layout.activity_delete_plan;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    private void initToolbar() {
        setBackArrow();
        setTitle("删除业务计划");

    }

    @Override
    public void initData() {
        initGetIntent();
    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
        }
    }

    /**
     * 接口5：删除业务计划（whm）
     */
    private void postWorkPlanlist() {
        if (TextUtils.isEmpty(etDelremark.getText())){
            ToastUtil.showShort("请填写删除说明");
            return;
        }
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");
        map.put("bid", id + "");
        map.put("del_remark", etDelremark.getText().toString().trim());
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkDelplan(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkDelplanInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(WorkDelplanInfo info) {
                        Logger.e("删除业务计划" + info.toString());
                        if (info.getCode() == 1) {
                            if (BusinessPlan2Activity.intance != null) {
                                BusinessPlan2Activity.intance.finish();
                            }
                            finish();
                        }
                        ToastUtil.showShort(null, info.getMsg() + "");
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    @OnClick(R.id.bt_delete)
    public void onViewClicked() {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        postWorkPlanlist();
    }
}
